GBML manual / Grids

grid_fill

Sets every cell of target to value. A grid is always full, so there is nothing to grow: the grid keeps its width and height and every cell becomes value. It works on a grid handed to a script too.

Syntax

grid_fill(target, value)

Arguments

NameType
targetgrid
valueelement

Returns

Nothing.

Description

Sets every cell of target, a grid, to value. A grid is always full, so there is nothing to grow: the grid keeps its width and its height, and every cell becomes value. Whatever was in the cells before is gone.

grid_fill(board, 0);      // every cell is now 0

To fill only some of the cells, use grid_fill_region.

Filling is not growing

list_fill makes a list longer, up to its capacity. A grid cannot grow, so grid_fill never changes how many cells there are. It only changes what they hold. That is why it is named the same, but it is a different kind of job.

It works in a script too

A script that takes a grid as a parameter works on the caller's own grid, so grid_fill inside a script fills the caller's grid, whatever size that is. Unlike grid_fill_region, it does not need the grid's width, because it goes through the cells one after another and does not care how they are laid out in rows. So it is the one grid function that works on a script's grid parameter.

What happens in the awkward cases

You do thisWhat happens
give a value of the wrong kind, like grid_fill(board, "x") for a grid of numbersthe project is refused when it is checked, and the message says what kind the grid holds
give it a list or an arrayrefused when checked. Use list_fill for a list. An array has no fill function
give it the wrong number of argumentsrefused when checked
use it where a value is needed, like var x = grid_fill(board, 0);refused when checked. grid_fill does a job and gives nothing back

value is worked out once and copied before any cell changes, so grid_fill(board, board[# 2, 3]) fills every cell with whatever cell 2, 3 held a moment ago.

What it costs

It visits every cell once, so the time grows with the size of the grid. Filling a small board when a level starts is nothing. Filling a big grid in every frame is a real part of the frame.

Example

var board = grid_create(8, 6, 0);
grid_fill(board, -1);       // every cell is -1
board[# 2, 3] = 5;          // one cell changed, the other 47 are still -1

On the GBA

A loop over every cell of the grid's vector, assigning one copy of value to each - O(W*H). value is worked out once and copied before the first cell changes, so grid_fill(board, board[# 2, 3]) fills with what that cell held a moment ago. It needs no width or height (it walks the cells, not the rows), so unlike the region functions it also works inside a script, on the CALLER's grid, whatever size that is: the grid parameter is a reference to the capacity-free vector base, which knows its own length. It never changes how many cells there are.

See also: grid_fill_regiongrid_creategrid_copygrid_widthgrid_heightlist_fillGridsData containersgrid_shufflegrid_foreach